package nio;

import sun.text.normalizer.UBiDiProps;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Summerday
 */
public class FileChannelTest {

    public static void main(String[] args) throws FileNotFoundException {

        // try-with-resources
        try (
                RandomAccessFile file = new RandomAccessFile("D://b.txt", "rw");
                FileChannel channel = file.getChannel()) {
            // 将channel中的所有数据映射为ByteBuffer
            ByteBuffer buf = channel.map(
                    FileChannel.MapMode.READ_WRITE, 0, file.length());
            int read = channel.read(buf);
            while(read != -1){
                System.out.println(" read => "+ read);
                buf.flip();
                while(buf.hasRemaining()){
                    System.out.println((char) buf.get());
                }
                buf.clear();
                read = channel.read(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
